﻿using Basic.UserCollect;
using Basic.UserModel.LoginLog;
using Basic.UserModel.User;
using Basic.UserModel.WxUser;
using Basic.UserRemoteModel;
using Basic.UserRemoteModel.WxLogin.Model;
using Basic.UserService.Interface;
using WeDonekRpc.Helper;

namespace Basic.UserService.Extend
{
    internal class WxMiniLoginService : IWxMiniLoginService
    {
        private readonly IUserCollect _User;
        private readonly IUserConfig _Config;
        private readonly ISaveLoginLog _LoginLog;
        private readonly IWxUserCollect _WxUser;
        private readonly IThirdAppCollect _ThirdApp;

        public WxMiniLoginService (IUserCollect user,
            IUserConfig config,
            ISaveLoginLog loginLog,
            IWxUserCollect wxUser,
            IThirdAppCollect thirdApp)
        {
            this._User = user;
            this._Config = config;
            this._LoginLog = loginLog;
            this._WxUser = wxUser;
            this._ThirdApp = thirdApp;
        }

        public MiniLoginResult Login (MiniLoginParam param)
        {
            int thirdId = this._ThirdApp.GetThirdId(param.WxAppId, this._Config.MiniAppType);
            WxUserDatum user = this._WxUser.WxUserLogin(new WxUserBase
            {
                ThirdId = thirdId,
                UnionId = param.UnionId,
                WxOpenId = param.WxOpenId
            });
            if (user.IsBind == false)
            {
                return new MiniLoginResult
                {
                    IsBind = false,
                    WxUserId = user.Id,
                    ThirdId = thirdId
                };
            }
            return this._WxLogin(user, thirdId, param);
        }
        private MiniLoginResult _WxLogin (WxUserDatum wxUser, int thirdId, MiniLoginParam param)
        {
            LoginLogAdd log = new LoginLogAdd
            {
                UserId = wxUser.BindUserId,
                LogSource = this._Config.MiniAppType,
                LoginIp = param.LoginIp,
                LoginName = param.WxOpenId,
                LoginType = LoginType.微信小程序,
                LoginTime = DateTime.Now,
                ThirdAppId = thirdId,
                ThirdUserId = wxUser.Id,
                UserAgent = param.UserAgent,
            };
            try
            {
                UserDatum user = this._User.UserLogin(wxUser.BindUserId);
                log.LogShow = string.Format("微信小程序用户登陆成功,昵称:{0}。", user.NickName);
                return new MiniLoginResult
                {
                    UserId = wxUser.BindUserId,
                    Sex = user.Sex,
                    NickName = user.NickName,
                    UserHead = user.UserHead,
                    IsBind = true,
                    WxUserId = user.Id,
                    ThirdId = thirdId
                };
            }
            catch (ErrorException ex)
            {
                log.LogShow = string.Format("微信小程序用户登陆失败,错误码:{0}。", ex.ErrorCode);
                throw;
            }
            finally
            {
                this._LoginLog.AddLog(log);
            }
        }
    }
}
